package com.cgx.tablet.beeper.utils;


import android.content.Context;

import com.cgx.tablet.beeper.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2018/9/10 0010.
 */

public class DateUtil {

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long time){
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(time);
    }

    /**
     * 获取时间差
     */
    public static long getDisTime(long createTime) {
        String create_time = stampToDate(createTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date createDate;
        Date nowDate;
        long diffSeconds = 0;
        long diff = 0;
        try {
            createDate = dateFormat.parse(create_time);
            nowDate = new Date();   //当前时间

            //毫秒ms
            diff = nowDate.getTime() - createDate.getTime();

            diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
//            if (diffDays > 0) {
////                if (diffDays / 365 > 0){
////                    return diffDays / 365 + "年前";
////                }else if (diffDays / 30 > 0){
////                    return diffDays / 30 + "月前";
////                }else if (diffDays / 7 > 0) {
////                    return diffDays / 7 + "周前";
////                } else {
////                    return diffDays + "天前";
////                }
//                return create_time;
//            } else if (diffHours > 0) {
//                return diffHours + "小时前";
//            } else if (diffMinutes > 0) {
//                return diffMinutes + "分钟前";
//            } else if (diffSeconds > 0) {
//                return diffSeconds + "秒前";
//            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return diff;
    }
    
    /**
     * 获取时间差
     */
    public static String getTime(Context context, int totalTime) {

        int minute = totalTime / 60;
        int hour = totalTime / 3600;
        if (hour > 0) {
            int a = totalTime - hour * 3600;            //减去小时剩下的分和秒
            int b = a / 60;                             //分
            if (b > 0) {
                int c = a - b * 60;
                return "" + (hour < 10 ? ("0" + hour) : hour) + (b < 10 ? ("0" + b) : b) +":" + (c < 10 ? ("0" + c) : c);
            } else
                return (hour < 10 ? ("0" + hour) : hour) + "00:" + (a < 10 ? ("0" + a) : a);
        } else if (minute > 0) {
            int a = totalTime - minute * 60;
            return "00:" + (minute < 10 ? ("0" + minute) : minute) +":" + (a < 10 ? ("0" + a) : a);
        } else
            return "00:00:" + (totalTime < 10 ? ("0" + totalTime) : totalTime);
    }
}
